/*
* 利用软串口使arduino同时连接姿态仪和数传
*/
#include <Wire.h>
#include <JY901.h>
#include <SoftwareSerial.h>
// 实例化软串口
// 将2，3端口分别定义为 RX, TX串口
SoftwareSerial mySerial(2, 3);
// 接收无线数传信息
String comingdata = "";

void setup()
{
  // 设置数传模块串口波特率
  Serial.begin(9600);
  Serial.println("Serial monitor starts successfully!");

  // 设置姿态仪模块串口波特率
  mySerial.begin(115200);
  // 软串口输出的信息无法在串口监视器上显示
  mySerial.println("Hello, world?");
}

void loop() 
{
  mySerial.listen();
  //print received data. Data was received in serialEvent;
  Serial.println("== reading data from attitude director ==");
  Serial.print("Angle:");Serial.print((float)JY901.stcAngle.Angle[0]/32768*180);Serial.print(" ");Serial.print((float)JY901.stcAngle.Angle[1]/32768*180);Serial.print(" ");Serial.println((float)JY901.stcAngle.Angle[2]/32768*180);
  delay(500);
  //
  while (Serial.available() > 0)  
  {
      comingdata += char(Serial.read());
      delay(2);
  }
  if (comingdata.length() > 0)
  {
      Serial.println("== reading data from data transfer ==");
      Serial.print(comingdata);
      comingdata = "";
  }
  
}

/*
  SerialEvent occurs whenever a new data comes in the
 hardware serial RX.  This routine is run between each
 time loop() runs, so using delay inside loop can delay
 response.  Multiple bytes of data may be available.
 */
void serialEvent() 
{
  while (mySerial.available()) 
  {
    JY901.CopeSerialData(mySerial.read()); //Call JY901 data cope function
    Serial.println("right");
  }
}
